package service

import (
	"sync"
)

var (
	// serviceInstance 是服务实例注册表
	serviceInstance = &serviceRegistry{
		services: make(map[interface{}]interface{}),
	}
)

// serviceRegistry 是服务注册表
type serviceRegistry struct {
	services map[interface{}]interface{}
	mu       sync.RWMutex
}

// RegisterService 注册服务实例
func RegisterService(serviceType interface{}, serviceImpl interface{}) {
	serviceInstance.mu.Lock()
	defer serviceInstance.mu.Unlock()
	serviceInstance.services[serviceType] = serviceImpl
}

// Get 获取服务实例
func (s *serviceRegistry) Get(serviceType interface{}) interface{} {
	s.mu.RLock()
	defer s.mu.RUnlock()
	if service, ok := s.services[serviceType]; ok {
		return service
	}
	return nil
}
